package org.linlinjava.litemall.wx.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.IliquormallTopic;
import org.linlinjava.litemall.db.service.ILiquormallTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/liquor/topic")
@Validated
@Api(tags="酒业资讯类")
public class WxIliquormallTopicController {
    private final Log logger = LogFactory.getLog(WxIliquormallTopicController.class);


    @Autowired
    private ILiquormallTopicService liquorTopicService;


    @ApiOperation("酒业资讯详情")
    @GetMapping("/detail")
    public Object detail(@RequestParam Integer id) {

        IliquormallTopic liquorTopic = liquorTopicService.selectById(id);

        if (liquorTopic.getReadCount() == null)
            liquorTopic.setReadCount(String.valueOf(0));

        // update read count
        liquorTopic = liquorTopicService.updateReadCount(liquorTopic);

        Map<String, Object> entity = new HashMap<>();
        entity.put("iLiquorTopic", liquorTopic);

        return ResponseUtil.ok(entity);
    }

    @ApiOperation("资讯列表返回")
    @GetMapping("/list")
    public Object list(@RequestParam String title,
                       @RequestParam Integer categoryId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        // TODO: 根据内容content搜索，需要在example里面加conent
        List<IliquormallTopic> liquorTopicList = liquorTopicService.queryLiquorTopicListByCategory(title, categoryId, page, limit, sort, order);
        Map<String, Object> entity = new HashMap<>();
        entity.put("iLiquormallTopicList", liquorTopicList);
        return ResponseUtil.ok(entity);
    }

}
